/**
 * Created by Administrator on 2018-09-06.
 * 进程节点配置
 */
define(function (require,exports,module) {
    var sTpl = require("./courseNodeBase.html");
    require("./courseNodeBase.css")
    var VueComponent = Vue.extend({
        template: sTpl
        ,methods:{
            processPreview:function () {
                var  $this=this;
                this.nodeData=[];
                var b={};
                this.dat.sort(function (va1,va2) {
                    return va1.nodeSeq > va2.nodeSeq?1:-1;
                });
                this.dat.forEach(function(value){
                    console.log(value)
                    var nodeSeq =value.nodeSeq+"a";
                  if(!b[nodeSeq]){
                      b[nodeSeq]={};
                  }
                      if(value.locatingPlace=='top'){
                          b[nodeSeq].top=value
                      }
                      if(value.locatingPlace=='bottom'){
                          b[nodeSeq].bottom=value
                      }
                })
               for(var i in b){
                   this.nodeData.push(b[i]);
               }
                this.ppDialogVisible=true;
            },
            findAllCnb:function() {
                var $this = this;
                axios.get(basepath+"/api/sysSetting/findCnb"
                ).then(function (response) {
                    $this.dat=response.data;
                    $this.tableData=response.data;
                });
            },

            addCnb:function(){
                this.addCnbData={
                    nodeName:'',
                    secUnit:'',
                    nodeSeq:'',
                    locatingPlace:''
                };
                this.tl='添加节点'
                this.$refs.baseTable.dialogFormVisible = true;
                this.title='添加成功';

            },
            clear:function(){
                this.$refs.baseTable.dialogFormVisible = false;

            },
            saveClick:function () {
                var $this = this;
                this.$refs.addCnbData.validate(function(validate){
                    if(validate){
                        axios.post(basepath+'/api/sysSetting/saveCnb',$this.addCnbData
                        )
                            .then(function (response) {
                                $this.$notify({
                                    title: $this.title,
                                    message: $this.title
                                });

                                $this.$refs.baseTable.dialogFormVisible = false;
                                $this.findAllCnb();
                            });
                    }
                });

            },
            editCnb:function (scope) {
                this.tl="节点修改"
                this.$refs.baseTable.dialogFormVisible = true;
                this.addCnbData=JSON.parse(JSON.stringify(scope.row)) ;
                this.title='修改成功';
            },
            delectCnb:function(scope){
                var $this = this ;
                this.$confirm('确定删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+'/api/sysSetting/delectCnb',scope.row)
                        .then(function (response) {
                            $this.$notify({
                                title: '已删除',
                                message: '已删除'
                            });

                            $this.findAllCnb();
                        });
                })

            }
        },
        mounted:function() {

        }
        ,data:function(){

            var checkInt = function(rule, value, callback){
                setTimeout(function()  {
                    if(value=='')callback(new Error('请输入节点顺序'));
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入合法数字'));
                    } else {
                        if (value <=0) {
                            callback(new Error('请输入合法数字'));
                        } else if(value>10000){
                            callback(new Error('请输入合理数字'));
                        }
                        else {
                            callback();
                        }
                    }
                }, 1000);
            };
            return {
                rules: {
                    nodeName: [
                        { required: true, message: '请输入节点名称', trigger: 'blur' },
                        { min: 1, max: 20, message: '名称太长', trigger: 'blur' }
                    ],
                    secUnit: [
                        { required: true, message: '请输入节点名称', trigger: 'blur' },
                        { min: 1, max: 20, message: '名称太长', trigger: 'blur' }
                    ],
                    nodeSeq: [
                        { validator: checkInt , trigger: 'blur' },
                        { required: true, message: '请输入节点顺序', trigger: 'blur' }
                    ],
                    locatingPlace:[
                        { required: true, message: '请选择节点摆放的位置', trigger: 'blur' },
                    ]
                },
                nodeDate:[],
                ppDialogVisible:false,
                locatingTy:[{
                    value:'top',
                    label:'居上'
                },{
                    value:'bottom',
                    label:'居下'
                }],
                tl:'',
                title:'',
                dialogFormVisible: false,
                addCnbData:{
                    nodeName:'',
                    secUnit:'',
                    nodeSeq:'',
                    locatingPlace:''
                },
                tableData: [],
                nodeData:[],
                dat:[]
            }
        },
        filters:{
            format:function (value) {
                if(value=='top') {
                    return '居上';
                }else if(value=='bottom'){
                    return '居下';
                }else{
                    return '';
                }
            }
        },
        computed:{
            time1:function () {
                var date=new Date();
                return date.getHours()+":"+date.getMinutes();
            }
        },
        created:function (){
            this.findAllCnb();

        },
        activated:function () {
            messages.$emit('change','进程节点配置');
        }
    });
    module.exports = VueComponent;
})
